<?php
/**
 * Created by PhpStorm.
 * User: liuzhiyan
 * Date: 2018/6/18
 * Time: 18:07
 */

namespace app\console\controller;

use app\BaseController;
use app\console\model\AdminRole;
use think\facade\Request;
use think\facade\Route;
use app\console\model\Admin as LoginModel;
use think\facade\Session;
use think\facade\View;
use think\captcha\facade\Captcha;
use app\middleware\CheckLogin;
use think\facade\Config;

class Login extends BaseController
{
//    protected $middleware = [
//        CheckLogin::class 	=> ['only' => 'index' ],
//
//    ];
    //protected $beforeActionList = ['checkLogin' => ['only' => 'index']];

    public function captcha(){
        echo 1234;
    }
    public function verify()
    {
        ob_clean();
        return Captcha::create('verify');
    }
    public function index()
    {

//        $gen =  gen();
//
//foreach($gen as $value){
//    dump($value);
//    echo "$value[1]<br/>";
//}
//return;
        $param = Request::param();
        $this->checkLogin();;
        if(Request::isPost()){
            if(isset($param['captcha']) && !Captcha::check(input('captcha')))
            {
                fail('验证码验证失败');
                // 验证失败
            }
            $ret = (new LoginModel())->login();

            //$captcha = new Captcha();



            if($ret !== true) fail($ret);
            $login_url = AdminRole::alias('ar')->where('ar.admin_id',Session::get('admin_id'))->join('Role r','ar.role_id = r.id')->value('login_url');
            jump('登录成功',Route::buildUrl($login_url));
            //jump('登录成功',Route::buildUrl('Dashboard/index'));
        }

        return View::fetch('', []);
    }

    public function company()
    {

        $param = Request::param();
        $this->checkLogin();;
        if(Request::isPost()){
            if(isset($param['captcha']) && !Captcha::check(input('captcha')))
            {
                fail('验证码验证失败');
                // 验证失败
            }
            $ret = (new LoginModel())->company();

            //$captcha = new Captcha();



            if($ret !== true) fail($ret);
            $login_url = AdminRole::alias('ar')->where('ar.admin_id',Session::get('admin_id'))->join('Role r','ar.role_id = r.id')->value('login_url');
            jump('登录成功',Route::buildUrl($login_url));
            //jump('登录成功',Route::buildUrl('Dashboard/index'));
        }

        return View::fetch('company', []);
    }

    public function out()
    {
        Session::delete('admin_id');
        Session::save();
        redirect(Route::buildUrl('console/login/index'))->send();
    }

    protected function checkLogin()
    {

        if (true === Session::has('admin_id')) {
            //login直接进来 进来后无权限 反复重定向无限;
            redirect(Route::buildUrl('console/Config/index'))->send();
        }
    }


}